package cn.com.dhcc.ddi.datasource.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.dbhandler.SingleTransationCircleWithOutResult;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.ddi.datasource.dao.DataSourceDao;
import cn.com.dhcc.ddi.datasource.dao.DataSourceOracleDao;
import cn.com.dhcc.ddi.datasource.vo.DataSourceVo;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
/**
 * 
 * oracle数据源服务类
 * @Author: fxx
 * @date: 2017-5-16
 *
 */
@Service
public class DataSourceOracleService extends BaseService {
	@Autowired
	DataSourceOracleDao dao;
	@Autowired
    DataSourceDao dataSourceDao;
	@Autowired
	DataSourceOracleDao dataSourceOracleDao;

/**
 * 	
 * @MethodName: [getUserTableForOracle]
 * @Description:[获取oracle用户表信息]
 * @Author:     [fxx]
 * @CreateDate: [2017-5-16]
 * @Params:     [ParamType ParamValue,ParamType ParamValue...]
 * @See         [Related classes and methods]
 * @Since       [Product template version]  
 * @Return:     [List<TableInfoVo>]
 * @Throws:     [Exception]
 *
 */
public List<TableInfoVo> getUserTableForOracle(DataSourceVo vo) throws ServiceException, NiceException{
		
		try {
			//根据数据源初始化dao
			dao.initDao(vo);
			final List<TableInfoVo> list = new ArrayList<TableInfoVo>();
			final List<TableInfoVo> userTableList  = getUserTablesForOracle(vo);
			dao.getDao().doInSingleTransationCircle("元数据抽取", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						for (Iterator<TableInfoVo> iterator = userTableList.iterator(); iterator.hasNext();) {
							TableInfoVo userTable = iterator.next();
							List<TableColVo> cols = new ArrayList<TableColVo>();
							cols = dao.getUserTabColumnsForOracle(userTable.getSchema().toUpperCase(), userTable.getTableName().toUpperCase());
							userTable.setTableColVos(cols);
							list.add(userTable);
						}
					} catch (DaoException e) {
						throw new RuntimeException("获取表元数据错误", e);
					}
				}
			});
			return list;
		} catch (DaoException e) {
			throw new ServiceException("获取表元数据错误", e);
		}
	}
/**
 * 抽取表结构，包括授权和自建的表和视图  Oracle
 * @param vo
 * @return
 * @throws ServiceException
 */
public List<TableInfoVo> getUserTablesForOracle(DataSourceVo vo) throws ServiceException{
	List<TableInfoVo> userTableList = new ArrayList<TableInfoVo>();
	try {
		//抽取授权的表和视图
		userTableList.addAll( dataSourceOracleDao.getToGrantTables(vo.getDataSourceConfVo().getUserName().toUpperCase()));
		//抽取自建的表和视图
		userTableList.addAll( dataSourceOracleDao.getUserTables(vo.getDataSourceConfVo().getUserName().toUpperCase()));
	} catch (DaoException e) {
		throw new ServiceException("获取表元数据错误", e);
	}
	return userTableList;
}

}
